{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import geemap\n",
    "import os"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "geemap.show_youtube('fDnDVuM_Ke4')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Update the geemap package\n",
    "\n",
    "If you run into errors with this notebook, please uncomment the line below to update the [geemap](https://github.com/giswqs/geemap#installation) package to the latest version from GitHub. \n",
    "Restart the Kernel (Menu -> Kernel -> Restart) to take effect."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# geemap.update_package()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Add animated text to an existing GIF\n",
    "\n",
    "You can download this GIF example from [here](https://github.com/giswqs/geemap/blob/master/examples/data/animation.gif). You can also create GIF images from Earth Engine data using this amazing [LT-GEE Time Series Animator](https://emaprlab.users.earthengine.app/view/lt-gee-time-series-animator), which was created by [Justin Braaten](https://github.com/jdbcode)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "in_gif = os.path.abspath('../data/animation.gif')\n",
    "out_gif = os.path.join(os.path.expanduser('~'), 'Downloads/output.gif')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "geemap.show_image(in_gif)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Add animated text to GIF"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "geemap.add_text_to_gif(in_gif, out_gif, xy=('5%', '5%'), text_sequence=1984, font_size=30, font_color='#0000ff', duration=100)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "geemap.show_image(out_gif)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Add place name"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "geemap.add_text_to_gif(out_gif, out_gif, xy=('30%', '85%'), text_sequence=\"Las Vegas\", font_color='black')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "geemap.show_image(out_gif)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Change font type"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "geemap.system_fonts()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "geemap.add_text_to_gif(in_gif, out_gif, xy=('5%', '5%'), text_sequence=1984, font_size=30, font_color='#0000ff', duration=100)\n",
    "geemap.add_text_to_gif(out_gif, out_gif, xy=('30%', '85%'), text_sequence=\"Las Vegas\",  font_type=\"timesbd.ttf\", font_size = 30, font_color='black')\n",
    "geemap.show_image(out_gif)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Create GIF from Earth Engine data\n",
    "\n",
    "This example was adapted from the Earth Engine JavaScript API Documentation [here](https://developers.google.com/earth-engine/ic_visualization#video_thumb). "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Prepare for an ImageCollection"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import ee\n",
    "import geemap\n",
    "\n",
    "ee.Initialize()\n",
    "\n",
    "# Define an area of interest geometry with a global non-polar extent.\n",
    "aoi = ee.Geometry.Polygon(\n",
    "  [[[-179.0, 78.0], [-179.0, -58.0], [179.0, -58.0], [179.0, 78.0]]], None, False)\n",
    "\n",
    "# Import hourly predicted temperature image collection for northern winter\n",
    "# solstice. Note that predictions extend for 384 hours; limit the collection\n",
    "# to the first 24 hours.\n",
    "tempCol = ee.ImageCollection('NOAA/GFS0P25') \\\n",
    "  .filterDate('2018-12-22', '2018-12-23') \\\n",
    "  .limit(24) \\\n",
    "  .select('temperature_2m_above_ground')\n",
    "\n",
    "# Define arguments for animation function parameters.\n",
    "videoArgs = {\n",
    "  'dimensions': 768,\n",
    "  'region': aoi,\n",
    "  'framesPerSecond': 10,\n",
    "  'crs': 'EPSG:3857',\n",
    "  'min': -40.0,\n",
    "  'max': 35.0,\n",
    "  'palette': ['blue', 'purple', 'cyan', 'green', 'yellow', 'red']\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Save the GIF to local drive"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "saved_gif = os.path.join(os.path.expanduser('~'), 'Downloads/temperature.gif')\n",
    "geemap.download_ee_video(tempCol, videoArgs, saved_gif)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "geemap.show_image(saved_gif)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Generate an hourly text sequence"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "text = [str(n).zfill(2) + \":00\" for n in range(0, 24)]\n",
    "print(text)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Add text to GIF"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "out_gif = os.path.join(os.path.expanduser('~'), 'Downloads/output2.gif')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "geemap.add_text_to_gif(saved_gif, out_gif, xy=('3%', '5%'), text_sequence=text, font_size=30, font_color='#ffffff')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "geemap.add_text_to_gif(out_gif, out_gif, xy=('32%', '92%'), text_sequence='NOAA GFS Hourly Temperature', font_color='white')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "geemap.show_image(out_gif)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
